/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.amuthu.indicator;

import org.amuthu.DataStore;

/**
 *
 * @author prabhu
 */
public abstract class Indicator
{
   /**
    *
    */
   protected int startOffset;
   /**
    *
    */
   private  double[] indicatorValue;
   /**
    *
    */
   private  DataStore dataStore;

   /**
    *
    */
   public Indicator()
   {
      startOffset = 0;
      dataStore = null;
      this.indicatorValue = new double[1];
   }

   /**
    *
    * @param dataStore
    * @param startOffset
    */
   protected Indicator(DataStore dataStore,
                       int startOffset)
   {
      this();
      this.dataStore = dataStore;
      this.startOffset = startOffset;
   }

   /**
    *
    * @return
    */
   public abstract double calculate();

   public abstract int getDayDependency();

   /**
    *
    * @return
    */
   public double[] getResultArray()
   {
      return indicatorValue;
   }

   /**
    *
    * @return
    */
   public double getResultValue()
   {
      return roundDouble(indicatorValue[0], 2);
   }

   /**
    *
    * @param d
    * @param places
    * @return
    */
   public static double roundDouble(double d,
                                    int places)
   {
      return Math.round(d * Math.pow(10, (double) places))
             / Math.pow(10,
                        (double) places);
   }

   /**
    *
    * @return
    */
   public int getLastIndex()
   {
      if (dataStore != null)
      {
         return dataStore.getSize() - startOffset - 1;
      }
      return -1;
   }

   @Override
   public String toString()
   {
      StringBuilder sb = new StringBuilder();
      sb.append(" value: ").append(getResultArray());
      return sb.toString();
   }

   /**
    * @return the dataStore
    */
   public DataStore getDataStore()
   {
      return dataStore;
   }

   /**
    * @param dataStore the dataStore to set
    */
   public void setDataStore(DataStore dataStore)
   {
      this.dataStore = dataStore;
   }

}
